package cmgine.com.testqq.TabBar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.Image;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.image.SmartImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmgine.com.testqq.R;
import cmgine.com.testqq.http.HttpDownloader;

public class MessageActivity extends AppCompatActivity {

    private ImageView avatarImageView;
    private EditText searchEditText;
    private static ListView listView;
    private Button settingBtn;
    private PopupWindow popupWindow;
    private List<Map<String, String>> moreList;
    private ListView popupListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        avatarImageView = (ImageView)findViewById(R.id.avatarImageView);
        avatarImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MessageActivity.this, ChatActivity.class);
                startActivity(intent);
            }
        });
        searchEditText = (EditText)findViewById(R.id.searchEditText);
        listView = (ListView)findViewById(R.id.listView);
        listView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                imm.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
                return false;
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MessageActivity.this, ChatActivity.class);
                startActivity(intent);
            }
        });

        initData();
        initPopupWindow();
        HttpDownloader httpDownloader = new HttpDownloader(this, listView);
        httpDownloader.execute();

        settingBtn = (Button)findViewById(R.id.settingBtn);
        settingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                } else {
                    popupWindow.showAsDropDown(settingBtn);
                }
            }
        });
    }

    private void initData() {
        moreList = new ArrayList<Map<String, String>>();
        Map<String, String> map;
        map = new HashMap<String, String>();
        map.put("share_key", "复制");
        moreList.add(map);
        map = new HashMap<String, String>();
        map.put("share_key", "删除");
        moreList.add(map);
        map = new HashMap<String, String>();
        map.put("share_key", "粘贴");
        moreList.add(map);
    }
    private void initPopupWindow() {
        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
        popupListView = (ListView)layout.findViewById(R.id.list_popupwindow);
        popupWindow = new PopupWindow(layout);
        popupWindow.setFocusable(true);

        popupListView.setAdapter(new SimpleAdapter(MessageActivity.this, moreList,
                R.layout.list_popupwindow_item,
                new String[]{"share_key"},
                new int[]{R.id.popwindow_list_item_text}));
        popupListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MessageActivity.this, moreList.get(position).get("share_key"), Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

        popupListView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupWindow.setWidth(popupListView.getMeasuredWidth());
        popupWindow.setHeight((popupListView.getMeasuredHeight() + 20) * moreList.size());
        popupWindow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_popupwindow));
        popupWindow.setOutsideTouchable(true);
    }
    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this).setTitle("确定退出吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MessageActivity.this.finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();
    }
}
